#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2022/9/26 11:35
# @Author  : CHENWang
# @Site    : 
# @File    : weekly_get_funding_rate_data.py
# @Software: PyCharm

"""
脚本说明:
"""

from TQR_Applications.factor_database.crypto.trading_data_update import get_funding_rate_data, get_composite_funding_rate
from logger import logger
import os
import traceback
from task_monitor import task_to_db, send_error_to_email


if __name__ == '__main__':
    # 获取各家交易所资金费率数据
    logger.info("开始get_funding_rate_data从coinglass")
    task_to_db(os.path.basename(__file__), 'get_funding_rate_data', 0, "coinglass")
    try:
        get_funding_rate_data(data_source='coinglass', end_date=None)
        task_to_db(os.path.basename(__file__), 'get_funding_rate_data', 1, "coinglass")
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="get_funding_rate_data", message=msg)
        # raise e
    # 计算综合资金费率
    logger.info("开始get_composite_funding_rate")
    task_to_db(os.path.basename(__file__), 'get_composite_funding_rate')
    try:
        get_composite_funding_rate()
        task_to_db(os.path.basename(__file__), 'get_composite_funding_rate', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="get_composite_funding_rate", message=msg)
        # raise e
    logger.info("成功get_composite_funding_rate")
    logger.info("开始get_funding_rate_data从binance")
    task_to_db(os.path.basename(__file__), 'get_funding_rate_data', 0, "binance")
    try:
        get_funding_rate_data(data_source='binance', end_date=None)
        task_to_db(os.path.basename(__file__), 'get_funding_rate_data', 1, "binance")
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="get_funding_rate_data", message=msg)
        # raise e
    logger.info("成功get_funding_rate_data从binance")